package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.ProductLibraryInfoDao;
import com.JZ.QM.common.entity.CombinationShareEntity;
import com.JZ.QM.common.entity.ProductLibraryInfoEntity;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;

import com.JZ.QM.common.dao.NewCombinationShareDao;
import com.JZ.QM.common.entity.NewCombinationShareEntity;
import com.JZ.QM.service.NewCombinationShareService;

import javax.annotation.Resource;

/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Service
public class NewCombinationShareServiceImpl extends ServiceImpl<NewCombinationShareDao, NewCombinationShareEntity> implements NewCombinationShareService {

    @Resource
    NewCombinationShareDao newCombinationShareDao;
    @Resource
    ProductLibraryInfoDao productLibraryInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<NewCombinationShareEntity> page = this.page(
                new Query<NewCombinationShareEntity>().getPage(params),
                new QueryWrapper<NewCombinationShareEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过用户修改后的组合来获取各个产品的比例字符串
     * @param comBaseId 用户修改后的组合id
     * @return
     */
    @Override
    public String getNewComDetail(Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //用户修改后的组合产品信息列表
        ArrayList<NewCombinationShareEntity> newCombinationShareEntities=new ArrayList<>();
        //产品代码列表
        ArrayList<String> proCodes=new ArrayList<>();
        //产品构造器
        QueryWrapper queryWrapperpro=new QueryWrapper();
        //备选库基本信息
        ProductLibraryInfoEntity productLibraryInfoEntity=new ProductLibraryInfoEntity();
        //组合标题字符串
        String result=new String();
        //将bigdecimal转换为字符串
        DecimalFormat df = new DecimalFormat("0.00%");

        NewCombinationShareEntity newCombinationShareEntity=new NewCombinationShareEntity();
        queryWrapper.select("pro_ts_code","new_com_share_radio")
                .eq("new_com_id",comBaseId);
        newCombinationShareEntities= (ArrayList<NewCombinationShareEntity>) newCombinationShareDao.selectList(queryWrapper);
        for (Iterator<NewCombinationShareEntity> it = newCombinationShareEntities.iterator(); it.hasNext(); ) {
            newCombinationShareEntity= it.next();
            queryWrapperpro.select("pro_ts_code","pro_name")
                    .eq("pro_ts_code",newCombinationShareEntity.getProTsCode());
            productLibraryInfoEntity=productLibraryInfoDao.selectOne(queryWrapperpro);
            result+=(productLibraryInfoEntity.getProName()+":"+df.format(newCombinationShareEntity.getNewComShareRadio())+";");
            queryWrapperpro.clear();
        }
        return result;


    }

    @Override
    public ArrayList<NewCombinationShareEntity> getCombinationShareList(Long comBaseId, int type) {
        //所有的成分产品
        ArrayList<NewCombinationShareEntity> TotalList = new ArrayList<>();
        //符合条件的成分产品
        ArrayList<NewCombinationShareEntity> resultList = new ArrayList<>();
        //类型判断flag
        boolean typeFlag = true;
        //条件构造器
        QueryWrapper<NewCombinationShareEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("new_com_id",comBaseId);

        TotalList = (ArrayList<NewCombinationShareEntity>)  newCombinationShareDao.selectList(queryWrapper);


        for (NewCombinationShareEntity newCombinationShareEntity:TotalList) {
            typeFlag = verifyIsStock(newCombinationShareEntity.getProTsCode(),type);
            if(typeFlag){
                resultList.add(newCombinationShareEntity);
            }
        }
        return resultList;
    }

    /**
     * 判断成分产品是不是股票
     * @param proTsCode
     * @return
     */
    public boolean verifyIsStock(String proTsCode,int type) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //备选库实体
        ProductLibraryInfoEntity productLibraryInfoEntity = new ProductLibraryInfoEntity();

        queryWrapper.eq("pro_ts_code",proTsCode);
        productLibraryInfoEntity = productLibraryInfoDao.selectOne(queryWrapper);

        if (productLibraryInfoEntity != null){
            if(productLibraryInfoEntity.getIsShare() == type){
                return true;
            }
        }
        return false;
    }

}